package com.game.base.server.crud;

import com.game.base.utils.log.Logger;

import java.util.List;

import cn.bmob.v3.BmobObject;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;

/**
 * 数据CRUD
 * Created by Su on 2019/5/20.
 */
public class CRUDMgr {
    private static volatile CRUDMgr mInstance;

    public synchronized static CRUDMgr getInstance() {
        if (mInstance == null) {
            synchronized (CRUDMgr.class) {
                if (mInstance == null) {
                    mInstance = new CRUDMgr();
                }
            }
        }
        return mInstance;
    }


    // ================== 数据存储， 自动建表 =====================================

    /**
     * 单条存储
     */
    public <T extends BmobObject> void saveSingle(T data, SaveListener<String> listener) {
        data.save(new SaveListener<String>() {
            @Override
            public void done(String s, BmobException e) {
                if (listener != null) {
                    listener.done(s, e);
                }
            }
        });
    }

    /**
     * 批量存储
     */
    public void saveBatch(List<? extends BmobObject> data) {
        if (data == null || data.size() <= 0) return;

        for (BmobObject o : data) {
            o.save(new SaveListener<String>() {
                @Override
                public void done(String objectId, BmobException e) {
                    if (e == null) {
                        Logger.d("添加数据成功，返回objectId为：" + objectId);
                    } else {
                        Logger.d("创建数据失败：" + e.getMessage());
                    }
                }
            });
        }
    }

    // ========================= 查询 - 目前无法封装==========================
    public <T extends BmobObject> void query(T data, Class<T> tClass) {
        BmobQuery<T> bmobQuery = new BmobQuery<>();
        bmobQuery.getObject("6f9aaa2445", new QueryListener<T>() {
            @Override
            public void done(T object, BmobException e) {
                if (e == null) {
                    Logger.d("查询成功");
                } else {
                    Logger.d("查询失败：" + e.getMessage());
                }
            }
        });
    }

    // ========================= 更新 ==========================

    public <T extends BmobObject> void update(T data) {
        data.update(data.getObjectId(), new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if (e == null) {
                    Logger.d("更新成功:");
                } else {
                    Logger.d("更新失败：" + e.getMessage());
                }
            }
        });
    }

    // ========================= 删除 ==========================
    public <T extends BmobObject> void delete(T data) {
        data.delete(new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if (e == null) {
                    Logger.d("删除成功:");
                } else {
                    Logger.d("删除失败：" + e.getMessage());
                }
            }
        });
    }
}
